//
//  BodyInfoController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/9/11.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "BodyInfoController.h"
#import "SelectController.h"
#import "ReSetController.h"
#import "DatePickerView.h"
@interface BodyInfoController ()<UITableViewDataSource,UITableViewDelegate,DatePickerViewDelegate>
@property(nonatomic,strong)UITableView * binfoTable;
@property(nonatomic,strong)NSArray * infoArray;
@property(nonatomic,strong)DatePickerView * pikerView;
@end

@implementation BodyInfoController
{
    UIButton * _backWindowView;
    NSString * _endTimeStr;
    NSString * _code;
    NSString * _gxmc;
    NSString * _cyxm;
    NSString * _csny;
    NSString * _NCYXM;
    NSString * _NCSNY;
    NSString * _mzm;
    NSString * _gjdqm;
    NSString * _jkzkmc;
    NSString * _zwm;
    NSString * _nxm;
    NSString * _gzdw;
    NSString * _dh;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _infoArray = @[@"关系",@"姓名",@"出生年月",@"民族",@"国籍",@"健康状况",@"工作单位",@"职位",@"电话"];
    [self createTable];
     self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonTitle:@"保存" image:nil frame:CGRectMake(0, 0, 30, 20) target:self sel:@selector(backClick)];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volue:) name:@"volue" object:nil];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MZM:) name:@"MZM" object:nil];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(GJDQM:) name:@"GJDQM" object:nil];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JKZKMC:) name:@"JKZKMC" object:nil];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ZYJSZWM:) name:@"ZYJSZWM" object:nil];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NCYXM:) name:@"NCYXM" object:nil];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CYGZDW:) name:@"CYGZDW" object:nil];
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DH:) name:@"DH" object:nil];
    
    _gxmc = self.homeInfoModel.GXM;
    _cyxm = self.homeInfoModel.CYXM;
    _csny = self.homeInfoModel.CSNY;
    _code = self.homeInfoModel.GXM;
    _endTimeStr = self.homeInfoModel.CSNY;
    _mzm = self.homeInfoModel.MZM;
    _gjdqm = self.homeInfoModel.GJDQM;
    _jkzkmc = self.homeInfoModel.JKZKM;
    _zwm = self.homeInfoModel.CYEM;
    _nxm = self.homeInfoModel.CYXM;
    _gzdw = self.homeInfoModel.CYGZDW;
    _dh = self.homeInfoModel.DH;
    
    if (_gxmc==nil) {
       _gxmc=@" ";
    }
    if (_cyxm==nil){
        _cyxm=@" ";
    }
    if (_csny==nil){
        _csny=@" ";
    }
    if (_code==nil){
        _code=@" ";
    }
    if (_endTimeStr==nil){
        _endTimeStr=@" ";
    }
    if (_mzm==nil){
        _mzm=@" ";
    }
    if (_gjdqm==nil){
        _gjdqm=@" ";
    }
    if (_jkzkmc==nil){
        _jkzkmc=@" ";
    }
    if (_zwm==nil){
        _zwm=@" ";
    }
    if (_nxm==nil){
        _nxm=@" ";
    }
    if (_gzdw==nil){
        _gzdw=@" ";
    }
    if (_dh==nil){
        _dh=@" ";
    }
}

-(void)createTable
{
    _binfoTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStyleGrouped];
    _binfoTable.delegate=self;
    _binfoTable.dataSource=self;
    _binfoTable.showsVerticalScrollIndicator=NO;
    _binfoTable.showsHorizontalScrollIndicator=NO;
    _binfoTable.userInteractionEnabled= YES ;
    
    _binfoTable.contentInset = UIEdgeInsetsMake(0, 0, -5, 0);
    
    _binfoTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_binfoTable];
}

#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.infoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    HomeBodyTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeBodyTableViewCell" owner:nil options:nil] lastObject];
    cell.titleLab.text = self.infoArray[indexPath.row];
    if (indexPath.row==0) {
        cell.nameLab.text = self.homeInfoModel.GXMC;
    }else if (indexPath.row==1){
        cell.nameLab.text = self.homeInfoModel.CYXM;
    }else if (indexPath.row==2){
        cell.nameLab.text = self.homeInfoModel.CSNY;
    }else if (indexPath.row==3){
        cell.nameLab.text = self.homeInfoModel.MZMC;
    }else if (indexPath.row==4){
        cell.nameLab.text = self.homeInfoModel.GJDQMC;
    }else if (indexPath.row==5){
        cell.nameLab.text = self.homeInfoModel.JKZKMC;
    }else if (indexPath.row==6){
        cell.nameLab.text = self.homeInfoModel.CYGZDW;
    }else if (indexPath.row==7){
        cell.nameLab.text = self.homeInfoModel.CYEMC;
    }else if (indexPath.row==8){
        cell.nameLab.text = self.homeInfoModel.DH;
    }
    return cell;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 43;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        SelectController *vc = [[SelectController alloc] init];
        vc.selectStr = @"NGXM";
        [self.navigationController pushViewController:vc animated:YES];

    }else if (indexPath.row==2){
        [self date];
    }else if (indexPath.row==3){
        SelectController *vc = [[SelectController alloc] init];
        vc.selectStr = @"MZM";
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row==4){
        SelectController *vc = [[SelectController alloc] init];
        vc.selectStr = @"GJDQM";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==5){
        SelectController *vc = [[SelectController alloc] init];
        vc.selectStr = @"JKZKMC";
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row==7){
        SelectController *vc = [[SelectController alloc] init];
        vc.selectStr = @"ZYJSZWM";
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
    NSArray * array = @[@"NGXM",@"NCYXM",@"NCSNY",@"MZM",@"GJDQM",@"JKZKMC",@"CYGZDW",@"ZWJBM",@"DH"];
    ReSetController *vc = [[ReSetController alloc] init];
    vc.reStr = array[indexPath.row];
        if (indexPath.row==1) {
             vc.restr = self.homeInfoModel.CYXM;
        }else if (indexPath.row==8){
         vc.restr = self.homeInfoModel.DH;
        }
    [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)date{
    if(_pikerView==nil){
        _backWindowView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        _backWindowView.backgroundColor = [UIColor blackColor];
        _backWindowView.alpha = 0.5;
        [_backWindowView addTarget:self action:@selector(backWindowClick) forControlEvents:UIControlEventTouchUpInside];
        
        [[UIApplication sharedApplication].keyWindow addSubview:_backWindowView];
        _pikerView = [DatePickerView datePickerView];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            //  设置日期格式
            [formatter setDateFormat:@"yyyy-MM-dd"];
            //  根据指定格式的字符串获取NSDate
            NSDate *maxDate = [formatter dateFromString:_endTimeStr];
            [_pikerView.datePickerView setMaximumDate:maxDate];
                _pikerView.delegate = self;
        
        _pikerView.type = 0;
        _pikerView.frame= CGRectMake(0, kScreen_Height, kScreen_Width, 257);
        [[UIApplication sharedApplication].keyWindow addSubview:_pikerView];
        
        [[_pikerView rac_signalForSelector:@selector(cannelBtnClick)]subscribeNext:^(id x) {
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [_backWindowView removeFromSuperview];
                _backWindowView = nil;
                _pikerView.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 257);
            } completion:^(BOOL finished) {
                [self.pikerView removeFromSuperview];
                self.pikerView = nil;
            }];
        }];
        
        
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            _pikerView.frame = CGRectMake(0, kScreen_Height-257, kScreen_Width, 257);
        } completion:^(BOOL finished) {
        }];
        
    }else{
        
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            [_backWindowView removeFromSuperview];
            _backWindowView = nil;
            _pikerView.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 257);
        } completion:^(BOOL finished) {
            [self.pikerView removeFromSuperview];
            self.pikerView = nil;
        }];
    }

}

-(void)backWindowClick
{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [_backWindowView removeFromSuperview];
        _backWindowView = nil;
        _pikerView.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 257);
    } completion:^(BOOL finished) {
        [self.pikerView removeFromSuperview];
        self.pikerView = nil;
    }];

}

-(void)backClick
{
    if ([self.typy isEqualToString:@"update"]) {
        NSDictionary *params = @{@"userid":[us objectForKey:kUserid],
                                 @"GXM":_gxmc,
                                 @"CYXM":_cyxm,
                                 @"CSNY":_csny,
                                 @"NGXM":_code,
                                 @"NCSNY":_endTimeStr,
                                 @"MZM":_mzm,
                                 @"GJDQM":_gjdqm,
                                 @"JKZKMC":_jkzkmc,
                                 @"CYEM":_zwm,
                                 @"NCYXM":_nxm,
                                 @"CYGZDW":_gzdw,
                                 @"DH":_dh
                                 };
        [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/updateHomeMember.do" withParaments:params withSuccessBlock:^(id object) {
            if ([object[@"message"]isEqualToString:@"更新成功"]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } withFailureBlock:^(NSError *error) {
            
            
        } progress:^(float progress) {
            
        }];

    }else if ([self.typy isEqualToString:@"add"]){
        if (![_nxm isEqualToString:@" "]&&_nxm!=nil&&![_code isEqualToString:@" "]&&_code!=nil&&![_endTimeStr isEqualToString:@" "]&&_endTimeStr!=nil&&![_mzm isEqualToString:@" "]&&_mzm!=nil&&![_gjdqm isEqualToString:@" "]&&_gjdqm!=nil&&![_jkzkmc isEqualToString:@" "]&&_jkzkmc!=nil&&![_zwm isEqualToString:@" "]&&_zwm!=nil&&![_gzdw isEqualToString:@" "]&&_gzdw!=nil&&![_dh isEqualToString:@" "]&&_dh!=nil) {
        NSDictionary *params = @{@"userid":[us objectForKey:kUserid],
                                 @"GXM":_code,
                                 @"CSNY":_endTimeStr,
                                 @"CYXM":_nxm,
                                 @"MZM":_mzm,
                                 @"GJDQM":_gjdqm,
                                 @"JKZKM":_jkzkmc,
                                 @"CYEM":_zwm,
                                 @"CYGZDW":_gzdw,
                                 @"DH":_dh
                                 };
        [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/addHomeMember.do" withParaments:params withSuccessBlock:^(id object) {
            
            if ([object[@"status"] boolValue]==1) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            
            
        } withFailureBlock:^(NSError *error) {
            
            
        } progress:^(float progress) {
            
        }];
            
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请将信息填写完整"
                                                                           message:nil
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定"
                                                             style:UIAlertActionStyleDefault
                                                           handler:nil];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
}

-(void)volue:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.GXMC = sender.userInfo[@"name"];
    _code = sender.userInfo[@"code"];
    [self.binfoTable reloadData];
}
-(void)MZM:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.MZMC = sender.userInfo[@"mz"];
    _mzm = sender.userInfo[@"mzm"];
    [self.binfoTable reloadData];
}
-(void)GJDQM:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.GJDQMC = sender.userInfo[@"gj"];
    _gjdqm = sender.userInfo[@"gjm"];
    [self.binfoTable reloadData];
}
-(void)JKZKMC:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.JKZKMC = sender.userInfo[@"jk"];
    _jkzkmc = sender.userInfo[@"jkm"];
    [self.binfoTable reloadData];
}
-(void)ZYJSZWM:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.CYEMC = sender.userInfo[@"zw"];
    _zwm = sender.userInfo[@"zwm"];
    [self.binfoTable reloadData];
}

-(void)NCYXM:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.CYXM = sender.userInfo[@"xm"];
    _nxm = sender.userInfo[@"xm"];
    [self.binfoTable reloadData];
}

-(void)CYGZDW:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.CYGZDW = sender.userInfo[@"gzdw"];
    _gzdw = sender.userInfo[@"gzdw"];
    [self.binfoTable reloadData];
}
-(void)DH:(NSNotification *)sender{
    //打印通知传过来的数值
    self.homeInfoModel.DH = sender.userInfo[@"dh"];
    _dh = sender.userInfo[@"dh"];
    [self.binfoTable reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getSelectDate:(NSString *)date type:(DateType)type {
    NSLog(@"%d - %@", type, date);
    
    
    NSString * strr = [NSString stringWithFormat:@"%@", date];
    _endTimeStr = [strr stringByReplacingOccurrencesOfString:@"-" withString:@""];
    self.homeInfoModel.CSNY = _endTimeStr;
    [_binfoTable reloadData];
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            [_backWindowView removeFromSuperview];
            _backWindowView = nil;
            _pikerView.frame = CGRectMake(0, kScreen_Height, kScreen_Width, 257);
        } completion:^(BOOL finished) {
            [self.pikerView removeFromSuperview];
            self.pikerView = nil;
        }];
    
}


@end
